layui.define(['table', 'form','table'], function(exports) {
    var $ = layui.$, table = layui.table, form = layui.form, table = layui.table;

    table.render({
        elem: '#LAY-table'
        , url: layui.setter.webRoot + '/cdb/device/list.data'
        , method: 'post'
        , parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        , response: {statusCode: 200}
        , cols: [[
            {field: 'deviceSn', title: '设备序列号'}
            , {field: 'deviceTypeName', title: '设备类型'}
            , {field: 'slotTotal', title: '插座数'}
            , {field: 'iotNodeName', title: 'IOT节点'}
            , {field: 'versionNumber', title: '版本号'}
            , {
                field: 'place', title: '部署网点', templet: function (record) {
                    if (record.device) {
                        if (record.device.placeName) {
                            return record.device.placeName;
                        }
                    }
                    return '---'
                }
            }
            , {
                field: 'userName', title: '设备归属', templet: function (record) {
                    if (record.device) {
                        if (record.device.userName) {
                            return record.device.userName + '(' + record.device.userPhone + ')';
                        }
                    }
                    return '---'
                }
            }
            , {
                field: 'stateName', title: '状态', templet: function (record) {
                    if (record.state == 2) {
                        return '<span class="layui-badge layui-bg-green">' + record.stateName + '</span>';
                    }
                    return record.stateName;
                }
            }
            , {field: 'rssi', title: '信号强度'}
            , {field: 'lastOfflineDate', title: '上次离线时间'}
            , {title: '操作', width: 200, align: 'center', fixed: 'right', templet: function (record) {
                    var html = '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="info">查看</a>';
                    html = html + '<a class="layui-btn layui-btn-xs" lay-event="qrcode">二维码</a>';
                    if(record.device.placeId){
                        html = html + ' <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="remove">移除部署</a>';
                    }else{
                        html = html + ' <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                    }
                    return html;
                }}
        ]]
        , page: true
        , limit: 30
        , height: 'full-220'
        , text: '对不起，加载出现异常！'
    });

    //监听搜索
    form.on('submit(LAY-search)', function (data) {
        var field = data.field;
        //执行重载
        table.reload('LAY-table', {
            where: field
            , page: {curr: 1}
        });
    });

    //事件
    var active = {
        add: function () {
            layer.open({
                type: 2
                , title: '新增共享充电宝机柜'
                , content: layui.setter.webRoot + '/cdb/device/add.html'
                , maxmin: true
                , area: ['500px', '500px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-form-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/cdb/device/add.do',
                            data: field,
                            dataType: "json",
                            success: function (res) {
                                if (res.code == 200) {
                                    table.reload('LAY-table');
                                    layer.close(index);
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        , info: function (obj) {
            layer.open({
                type: 2
                , title: '共享充电宝机柜详情'
                , content: layui.setter.webRoot + '/cdb/device/info.html?deviceSn=' + obj.data.deviceSn
                , maxmin: true
                , area: ['90%', '80%']
            });
        }
        , qrcode: function (obj) {
            layer.open({
                type: 2
                , title: '设备二维码'
                , content: layui.setter.webRoot + '/cdb/device/qrcode.html?deviceSn=' + obj.data.deviceSn
                , maxmin: false
                , area: ['400px', '450px']
                , btn: ['取消']
            });
        }
        ,remove:function(obj){
            parent.layer.confirm('确定要移除该设备部署吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/place/device/undeploy.do',
                    data:{deviceSn:obj.data.deviceSn},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            obj.del();
                        }else{
                            parent.layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                parent.layer.close(index);
            });
        }
        , del: function (obj) {
            layer.confirm('确定要删除该共享充电宝机柜吗？', function (index) {
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/cdb/device/del.do',
                    data: {deviceSn: obj.data.deviceSn},
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 200) {
                            obj.del();
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    };

    $('.layui-btn.LAY-btn-fun').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-table)', function (obj) {
        var type = obj.event;
        active[type] ? active[type].call(this, obj) : '';
    });

    exports('battery_cabinet_list', {})
});